package co.edu.usbcali.presentation.backingBeans;

import co.edu.usbcali.exceptions.*;
import co.edu.usbcali.modelo.*;
import co.edu.usbcali.modelo.dto.ValidacionDTO;
import co.edu.usbcali.presentation.businessDelegate.*;
import co.edu.usbcali.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.RowEditEvent;

import java.io.Serializable;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;


/**
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
@ManagedBean
@ViewScoped
public class ValidacionView implements Serializable {
    private static final long serialVersionUID = 1L;
    private InputText txtDatacredito;
    private InputText txtListaclinton;
    private InputText txtPuntajecifin;
    private InputText txtIdsoli_Solicitud;
    private InputText txtIdvali;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<ValidacionDTO> data;
    private ValidacionDTO selectedValidacion;
    private Validacion entity;
    private boolean showDialog;
    @ManagedProperty(value = "#{BusinessDelegatorView}")
    private IBusinessDelegatorView businessDelegatorView;

    public ValidacionView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            ValidacionDTO validacionDTO = (ValidacionDTO) e.getObject();

            if (txtDatacredito == null) {
                txtDatacredito = new InputText();
            }

            txtDatacredito.setValue(validacionDTO.getDatacredito());

            if (txtListaclinton == null) {
                txtListaclinton = new InputText();
            }

            txtListaclinton.setValue(validacionDTO.getListaclinton());

            if (txtPuntajecifin == null) {
                txtPuntajecifin = new InputText();
            }

            txtPuntajecifin.setValue(validacionDTO.getPuntajecifin());

            if (txtIdsoli_Solicitud == null) {
                txtIdsoli_Solicitud = new InputText();
            }

            txtIdsoli_Solicitud.setValue(validacionDTO.getIdsoli_Solicitud());

            if (txtIdvali == null) {
                txtIdvali = new InputText();
            }

            txtIdvali.setValue(validacionDTO.getIdvali());

            Long idvali = FacesUtils.checkLong(txtIdvali);
            entity = businessDelegatorView.getValidacion(idvali);

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_new() {
        action_clear();
        selectedValidacion = null;
        setShowDialog(true);

        return "";
    }

    public String action_clear() {
        entity = null;
        selectedValidacion = null;

        if (txtDatacredito != null) {
            txtDatacredito.setValue(null);
            txtDatacredito.setDisabled(true);
        }

        if (txtListaclinton != null) {
            txtListaclinton.setValue(null);
            txtListaclinton.setDisabled(true);
        }

        if (txtPuntajecifin != null) {
            txtPuntajecifin.setValue(null);
            txtPuntajecifin.setDisabled(true);
        }

        if (txtIdsoli_Solicitud != null) {
            txtIdsoli_Solicitud.setValue(null);
            txtIdsoli_Solicitud.setDisabled(true);
        }

        if (txtIdvali != null) {
            txtIdvali.setValue(null);
            txtIdvali.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        return "";
    }

    public void listener_txtId() {
        try {
            Long idvali = FacesUtils.checkLong(txtIdvali);
            entity = (idvali != null)
                ? businessDelegatorView.getValidacion(idvali) : null;
        } catch (Exception e) {
            entity = null;
        }

        if (entity == null) {
            txtDatacredito.setDisabled(false);
            txtListaclinton.setDisabled(false);
            txtPuntajecifin.setDisabled(false);
            txtIdsoli_Solicitud.setDisabled(false);
            txtIdvali.setDisabled(false);
            btnSave.setDisabled(false);
        } else {
            txtDatacredito.setValue(entity.getDatacredito());
            txtDatacredito.setDisabled(false);
            txtListaclinton.setValue(entity.getListaclinton());
            txtListaclinton.setDisabled(false);
            txtPuntajecifin.setValue(entity.getPuntajecifin());
            txtPuntajecifin.setDisabled(false);
            txtIdsoli_Solicitud.setValue(entity.getSolicitud().getIdsoli());
            txtIdsoli_Solicitud.setDisabled(false);
            txtIdvali.setValue(entity.getIdvali());
            txtIdvali.setDisabled(true);
            btnSave.setDisabled(false);

            if (btnDelete != null) {
                btnDelete.setDisabled(false);
            }
        }
    }

    public String action_edit(ActionEvent evt) {
        selectedValidacion = (ValidacionDTO) (evt.getComponent().getAttributes()
                                                 .get("selectedValidacion"));
        txtDatacredito.setValue(selectedValidacion.getDatacredito());
        txtDatacredito.setDisabled(false);
        txtListaclinton.setValue(selectedValidacion.getListaclinton());
        txtListaclinton.setDisabled(false);
        txtPuntajecifin.setValue(selectedValidacion.getPuntajecifin());
        txtPuntajecifin.setDisabled(false);
        txtIdsoli_Solicitud.setValue(selectedValidacion.getIdsoli_Solicitud());
        txtIdsoli_Solicitud.setDisabled(false);
        txtIdvali.setValue(selectedValidacion.getIdvali());
        txtIdvali.setDisabled(true);
        btnSave.setDisabled(false);
        setShowDialog(true);

        return "";
    }

    public String action_save() {
        try {
            if ((selectedValidacion == null) && (entity == null)) {
                action_create();
            } else {
                action_modify();
            }

            data = null;
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_create() {
        try {
            entity = new Validacion();

            Long idvali = FacesUtils.checkLong(txtIdvali);

            entity.setDatacredito(FacesUtils.checkFloat(txtDatacredito));
            entity.setIdvali(idvali);
            entity.setListaclinton(FacesUtils.checkString(txtListaclinton));
            entity.setPuntajecifin(FacesUtils.checkFloat(txtPuntajecifin));
            entity.setSolicitud((FacesUtils.checkLong(txtIdsoli_Solicitud) != null)
                ? businessDelegatorView.getSolicitud(FacesUtils.checkLong(
                        txtIdsoli_Solicitud)) : null);
            businessDelegatorView.saveValidacion(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            entity = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            if (entity == null) {
                Long idvali = new Long(selectedValidacion.getIdvali());
                entity = businessDelegatorView.getValidacion(idvali);
            }

            entity.setDatacredito(FacesUtils.checkFloat(txtDatacredito));
            entity.setListaclinton(FacesUtils.checkString(txtListaclinton));
            entity.setPuntajecifin(FacesUtils.checkFloat(txtPuntajecifin));
            entity.setSolicitud((FacesUtils.checkLong(txtIdsoli_Solicitud) != null)
                ? businessDelegatorView.getSolicitud(FacesUtils.checkLong(
                        txtIdsoli_Solicitud)) : null);
            businessDelegatorView.updateValidacion(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_datatable(ActionEvent evt) {
        try {
            selectedValidacion = (ValidacionDTO) (evt.getComponent()
                                                     .getAttributes()
                                                     .get("selectedValidacion"));

            Long idvali = new Long(selectedValidacion.getIdvali());
            entity = businessDelegatorView.getValidacion(idvali);
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_master() {
        try {
            Long idvali = FacesUtils.checkLong(txtIdvali);
            entity = businessDelegatorView.getValidacion(idvali);
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public void action_delete() throws Exception {
        try {
            businessDelegatorView.deleteValidacion(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data = null;
        } catch (Exception e) {
            throw e;
        }
    }

    public String action_closeDialog() {
        setShowDialog(false);
        action_clear();

        return "";
    }

    public String actionDeleteDataTableEditable(ActionEvent evt) {
        try {
            selectedValidacion = (ValidacionDTO) (evt.getComponent()
                                                     .getAttributes()
                                                     .get("selectedValidacion"));

            Long idvali = new Long(selectedValidacion.getIdvali());
            entity = businessDelegatorView.getValidacion(idvali);
            businessDelegatorView.deleteValidacion(entity);
            data.remove(selectedValidacion);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Float datacredito, Long idvali,
        String listaclinton, Float puntajecifin, Long idsoli_Solicitud)
        throws Exception {
        try {
            entity.setDatacredito(FacesUtils.checkFloat(datacredito));
            entity.setListaclinton(FacesUtils.checkString(listaclinton));
            entity.setPuntajecifin(FacesUtils.checkFloat(puntajecifin));
            businessDelegatorView.updateValidacion(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("ValidacionView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtDatacredito() {
        return txtDatacredito;
    }

    public void setTxtDatacredito(InputText txtDatacredito) {
        this.txtDatacredito = txtDatacredito;
    }

    public InputText getTxtListaclinton() {
        return txtListaclinton;
    }

    public void setTxtListaclinton(InputText txtListaclinton) {
        this.txtListaclinton = txtListaclinton;
    }

    public InputText getTxtPuntajecifin() {
        return txtPuntajecifin;
    }

    public void setTxtPuntajecifin(InputText txtPuntajecifin) {
        this.txtPuntajecifin = txtPuntajecifin;
    }

    public InputText getTxtIdsoli_Solicitud() {
        return txtIdsoli_Solicitud;
    }

    public void setTxtIdsoli_Solicitud(InputText txtIdsoli_Solicitud) {
        this.txtIdsoli_Solicitud = txtIdsoli_Solicitud;
    }

    public InputText getTxtIdvali() {
        return txtIdvali;
    }

    public void setTxtIdvali(InputText txtIdvali) {
        this.txtIdvali = txtIdvali;
    }

    public List<ValidacionDTO> getData() {
        try {
            if (data == null) {
                data = businessDelegatorView.getDataValidacion();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<ValidacionDTO> validacionDTO) {
        this.data = validacionDTO;
    }

    public ValidacionDTO getSelectedValidacion() {
        return selectedValidacion;
    }

    public void setSelectedValidacion(ValidacionDTO validacion) {
        this.selectedValidacion = validacion;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }

    public IBusinessDelegatorView getBusinessDelegatorView() {
        return businessDelegatorView;
    }

    public void setBusinessDelegatorView(
        IBusinessDelegatorView businessDelegatorView) {
        this.businessDelegatorView = businessDelegatorView;
    }

    public boolean isShowDialog() {
        return showDialog;
    }

    public void setShowDialog(boolean showDialog) {
        this.showDialog = showDialog;
    }
}
